package com.animal.controller;


import com.animal.auto.JSONAuthentication;

import com.animal.entity.AnMeans;
import com.animal.mapper.AnMeansMapper;
import com.animal.response.Result;
import com.animal.utils.UpPhotoNameUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author animal
 * @since 2022-02-06
 */
@RestController
@RequestMapping("/an-means")
public class AnMeansController extends JSONAuthentication {
    @Autowired
    AnMeansMapper anMeansMapper;


    @ApiOperation(value = "添加资料")
    @PostMapping("/add")
    public void addMean(@RequestParam("file") MultipartFile file, HttpServletRequest request,HttpServletResponse response,@RequestParam("userId") int userId,@RequestParam("userAddress") String userAddress,@RequestParam("diliverAddress") String diliverAddress,@RequestParam("phone") String phone,@RequestParam("certificate") String certificate ) throws  Exception{
        AnMeans anMeans=new AnMeans();
        anMeans.setUserId(userId);
        anMeans.setUserAddress(userAddress);
        anMeans.setDiliverAddress(diliverAddress);
        anMeans.setPhone(phone);
        anMeans.setCertificate(certificate);
        byte[] bytes = file.getBytes();
        String imageFileName = file.getOriginalFilename();
        String fileName = UpPhotoNameUtils.getPhotoName("img",imageFileName);
        Path path = Paths.get("D:\\自己的项目\\流浪动物救助\\adopt\\adopt\\src\\assets\\picture\\" + fileName);
        //“C:\\框架\\D4\\d4_pc_ui\\src\\assets\\images\\img\\”为本地目录
        Files.write(path, bytes);//写入文件
        String avatar_url=fileName;
        anMeans.setCertificatePicture(avatar_url);
        response.reset();

        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        anMeans.setCreatTime(timestamp);

        if(anMeansMapper.findByUserId(anMeans.getUserId())!=null){
            Result result=Result.error();
            result.setMessage("该用户资料已存在");
            this.WriteJSON(request, response, result);
        }else{
            int i=anMeansMapper.insert(anMeans);
            if(i>0){
                Result ok = Result.ok();
                this.WriteJSON(request, response, ok);
            } else {
                Result error = Result.error();
                this.WriteJSON(request, response, error);
            }
        }

    }

    @ApiOperation(value = "删除资料")
    @PostMapping("/del")
    public void delMean(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id ) throws Exception{
        int i=anMeansMapper.deleteById(id);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "修改资料内容")
    @PostMapping("/upd")
    public void updMean(@RequestParam("file") MultipartFile file, HttpServletRequest request,HttpServletResponse response,@RequestParam("id") int id,@RequestParam("userId") int userId,@RequestParam("userAddress") String userAddress,@RequestParam("diliverAddress") String diliverAddress,@RequestParam("phone") String phone,@RequestParam("certificate") String certificate )throws Exception{
        AnMeans anMeans=new AnMeans();
        anMeans.setId(id);
        anMeans.setUserId(userId);
        anMeans.setUserAddress(userAddress);
        anMeans.setDiliverAddress(diliverAddress);
        anMeans.setPhone(phone);
        anMeans.setCertificate(certificate);
        byte[] bytes = file.getBytes();
        String imageFileName = file.getOriginalFilename();
        String fileName = UpPhotoNameUtils.getPhotoName("img",imageFileName);
        Path path = Paths.get("D:\\自己的项目\\流浪动物救助\\adopt\\adopt\\src\\assets\\picture\\" + fileName);
        //“C:\\框架\\D4\\d4_pc_ui\\src\\assets\\images\\img\\”为本地目录
        Files.write(path, bytes);//写入文件
        String avatar_url=fileName;
        anMeans.setCertificatePicture(avatar_url);
        response.reset();

        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        anMeans.setUpdTime(timestamp);

        int i=anMeansMapper.updateById(anMeans);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }


    }
    @ApiOperation(value = "查询资料")
    @PostMapping("/look")
    public void lookAnimal(HttpServletRequest request, HttpServletResponse response,@RequestParam("userId") int userId)throws  Exception{
       AnMeans anMeans= anMeansMapper.findByUserId(userId);

        Result ok = Result.ok().data("AnInvitation",anMeans);

        this.WriteJSON(request, response, ok);
    }

    @ApiOperation(value = "修改资料状态")
    @PostMapping("/updStatus")
    public void updMeanstatus( HttpServletRequest request,HttpServletResponse response,@RequestParam("id") int id,@RequestParam("certificateType") int certificateType)throws Exception{
        AnMeans anMeans=anMeansMapper.selectById(id);
        anMeans.setCertificateType(certificateType);

        int i=anMeansMapper.updateById(anMeans);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }


    }

}

